package cn.jdy.xyajj.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.User;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.djgaj.entity.DjgajNews;
import cn.jdy.djgaj.sevice.DjgajNewsService;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.xyajj.entity.XyajjCompany;
import cn.jdy.xyajj.service.XyajjCompanyService;

/**
 * @title  襄阳市安全生产监督管理局-企业信息管理操作
 * @author ningqian
 * @date   2017-2-22 11:24:00
 */
@Controller
@RequestMapping("/xyajjcompany")
public class XyajjCompanyCtrl extends BaseCtrl{


	@Autowired
	private XyajjCompanyService xyajjCompanyService;
	//默认列表路径
	private static String viewName = "redirect:/xyajjcompany/list?s=";

	/**
	 * 初始化
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(XyajjCompany entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/operate/companyList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
		mv.addObject("entity", entity);
		mv.addObject("fromDate", fromDate);
		mv.addObject("toDate", toDate);
		return mv;
	}

	/**
	 * 异步查询
	 * @param company
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(XyajjCompany company, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", xyajjCompanyService.query(company, fromDate,toDate));
			jsonMap.put("iTotalDisplayRecords", xyajjCompanyService.count(company, fromDate, toDate));
			jsonMap.put("iTotalRecords", xyajjCompanyService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}

	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}

	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/operate/companyDetail.jsp");
		mv.addObject("data", id==null||id==0 ? id : this.xyajjCompanyService.find(id));
		return mv;
	}
  
	
	/**
	 * 保存（更新）企业信息
	 * @param company
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(XyajjCompany company) {
		ModelAndView mv = new ModelAndView();
		try {
			if(null == company.getId()) {
				// 新增企业信息
				this.xyajjCompanyService.save(company);
				mv.setViewName(this.viewName.concat("1"));
			} else {
				//更新企业信息
				this.xyajjCompanyService.update(company);
				mv.setViewName(this.viewName.concat("2"));
		    // 清理文章缓存
		    NginxPurgeCacheUtil.purge(2, company.getId());
			}
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "保存异常！", e);
		}

		return mv;
	}
	
	/**
	 * 删除企业信息
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.xyajjCompanyService.delete(id);
			mv.setViewName(this.viewName.concat("3"));
			// 清理文章缓存
			NginxPurgeCacheUtil.purge(2, id);
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "删除异常！", e);
		}
		return mv;
	}

}
